/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "HttpUrl.h"
#include "openssl/md5.h"
#include <web/arkweb_scheme_handler.h>


string HttpUrl::GetMimeType(const string& strFilePath)
{
    if(strFilePath.find(".html") != string::npos) {
         return "text/html";
    }else if(strFilePath.find(".js") != string::npos) {
        return "text/javascript";
    }else if(strFilePath.find(".css") != string::npos) {
        return "text/css";
    }else if(strFilePath.find(".jpg") != string::npos) {
        return "image/jpeg";
    }else if(strFilePath.find(".jpeg") != string::npos) {
        return "image/jpeg";
    }else if(strFilePath.find(".png") != string::npos) {
        return "image/png";
    }else if(strFilePath.find(".svg") != string::npos) {
        return "image/svg+xml";
    }else if(strFilePath.find(".ico") != string::npos) {
        return "image/x-icon";
    }else if(strFilePath.find(".woff") != string::npos) {
        return "font/woff";
    }else if(strFilePath.find(".ttf") != string::npos) {
        return "font/ttf";
    }else if(strFilePath.find(".mp4") != string::npos) {
        return "video/mp4";
    } else if(strFilePath.find(".mp3") != string::npos) {
        return "video/mp3";
    } else if(strFilePath.find(".3gp") != string::npos) {
        if(strFilePath.find("audio") != string::npos)
            return "audio/3ggp";
        else
          return "video/3ggp";  
    }  else if(strFilePath.find(".json") != string::npos) {
        return "application/json";
    } else if(strFilePath.find(".m4a") != string::npos) {
        return "audio/x-m4a";
    } else if(strFilePath.find(".ogg") != string::npos) {
        return "audio/ogg";
    } 
    return "text/html";
}

string HttpUrl::GetMimeType(const int resourceType, const string& strUrlPath)
{
    if(resourceType == MAIN_FRAME) {
        return "text/html";
    } else if(resourceType == SUB_FRAME) {
        return "text/html";
    } else if(resourceType == STYLE_SHEET){
        return "text/css";
    } else if(resourceType == SCRIPT){
        return "text/javascript";
    } else if(resourceType == IMAGE){
        if(strUrlPath.find(".jpg") != string::npos) {
            return "image/jpeg";
        } else if(strUrlPath.find(".jpeg") != string::npos) {
            return "image/jpeg";
        } else if(strUrlPath.find(".png") != string::npos) {
            return "image/png";
        } else if(strUrlPath.find(".svg") != string::npos) {
            return "image/svg+xml";
        } else {
            return "image/jpeg";
        }
    } else if(resourceType == FONT_RESOURCE){
        if(strUrlPath.find(".woff") != string::npos) {
            return "font/woff";
        } else if(strUrlPath.find(".ttf") != string::npos) {
            return "font/ttf";
        } else {
            return "font/ttf";
        }
    } else if(resourceType == FAVICON){
        return "image/x-icon";
    } else if(resourceType == MEDIA){
        if(strUrlPath.find(".mp4") != string::npos) {
            return "video/mp4";
        } else if(strUrlPath.find(".mp3") != string::npos) {
            return "video/mp3";
        } else if(strUrlPath.find(".m4a") != string::npos) {
            return "audio/x-m4a";
        } else if(strUrlPath.find(".ogg") != string::npos) {
           return "audio/ogg";
        } else if(strUrlPath.find(".3gp") != string::npos) {
           return "audio/3ggp";
        }
        return "image/x-icon";
    } else {
        return "text/html";
    }
}

string HttpUrl::trim(const string & strValue)
{
    string strRet = strValue;
    strRet.erase(0, strRet.find_first_not_of(" "));
    strRet.erase(strRet.find_last_not_of(" ") + 1);
    strRet.erase(0, strRet.find_first_not_of("\n"));
    strRet.erase(strRet.find_last_not_of("\n") + 1);
    strRet.erase(0, strRet.find_first_not_of("\r"));
    strRet.erase(strRet.find_last_not_of("\r") + 1);
    return strRet;
}


void HttpUrl::SplitString(const string& strString,const string & split,  vector<string>& vecString)
{
    int pos = -1;
    string strSrcTmp = strString;
    if(strString.empty())
        return;
    string strTmp;
    while(true)
    {
        pos = strSrcTmp.find(split);
        if(pos < 0)
                break;
        strTmp = strSrcTmp.substr(0, pos);
        vecString.push_back(strTmp);
        strSrcTmp = strSrcTmp.substr(pos+1);
    }
    vecString.push_back(strSrcTmp);
    return;
}


string HttpUrl::generateMD5(const string& strIn) 
{
    MD5_CTX context;
    MD5_Init(&context);                      // 初始化 MD5 上下文
    MD5_Update(&context, strIn.c_str(), strIn.length()); // 添加输入数据
    unsigned char md5Hash[MD5_DIGEST_LENGTH];
    char md5Value[2*MD5_DIGEST_LENGTH+1];
    MD5_Final(md5Hash, &context); 
    for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
        sprintf(md5Value+i+i, "%02x", md5Hash[i]); // 以十六进制格式打印每个字节
    }
    return md5Value;
}
